/**
 * CustomTranslationServiceImpl.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.services;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import extjsdyntran.servlets.Constants;
import extjsdyntran.translation.TranslationAbstract;

/**
 * @description Translation Service sample used to load custom language definitions from local properties file.
 * customlang.properties file will be created with list of languages, and language files listed there will be loaded 
 * 
 */
public class CustomTranslationServiceImpl extends TranslationAbstract {

	private static final long serialVersionUID = 1L;
	private Properties customlang = null; 

	public CustomTranslationServiceImpl(String username, String password,	String _path) {
		super(username, password, _path);
		customlang = new Properties();
		loadLanguages();
	}
	
	public boolean isNewLanguageSupported(){
		return true;
	}

	public boolean isLanguageSupported(String language){
		return customlang.containsKey(language);
	}
	
	@Override
	public void loadLanguages() {
		
		File file = new File(path+ "customlang.properties");
		if (file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				customlang.load(fis);
				fis.close();
				super.loadLanguages(super.enumerationToStringArray(customlang));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
		
	}
	
	@Override
	public boolean saveLanguages(String language) {	
		
		boolean result = super.saveLanguages(language);		
		if (! result) return result;


		if ("*".equals(language)) {

			try{		
				//save custom languages
				FileOutputStream fos = new FileOutputStream(path+ "customlang.properties");
				customlang.store(fos, "");
				fos.flush();
				fos.close();				
				
				return true;
			} catch (IOException e1) {
				e1.printStackTrace();
				return false;
			}
			}
		
		 return result;
		}

	@Override
	public boolean test() {
		return true;
	}


	@Override
	public boolean appendLanguageFromCache(String language) {
		//@todo call factory to check existence in other translation services		
		if ( customlang.containsKey(language)) {
			return false;
		}		
		
		customlang.put(language,language.substring(0, 3));
		Properties lang = new Properties();
		languages.put(language, lang);
		
		return true;
	}


	@Override
	public boolean removeLanguageFromCache(String language) {
				
		if (!customlang.containsKey(language)) {
			return false;
		}	
		
		customlang.remove(language);
		languages.remove(language);
		return true;
	}

	
	
	@Override
	public String translate(String text, String language) {
		try {

			currentLanguage = language;

			Properties lang = (Properties) languages.get(language);
			String result = null;
			if (lang == null) return text;
			
			result = (String) lang.get(text);
			if (result == null) {				
				lang.setProperty(text, text);
				result = text;
			}

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Constants.json_false;
		}
	}



	@Override
	public Properties getLanguages() {
		return customlang;
	}	
	
}